package controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import dao.CandidateDAORemote;
import dto.CandidateDTO;



@ManagedBean
@SessionScoped
public class CandidateController implements Serializable {
	private static final long serialVersionUID = 1L;

	@EJB
	CandidateDAORemote candidateDAO;

	@ManagedProperty(value = "#{candidateDTO}")
	CandidateDTO candidateDTO;
	
	@ManagedProperty(value = "#{testerController}")
	TesterController testerController;

	private List<String> checkbox = new ArrayList<String>();
	private List<CandidateDTO> listOfCandidates;
	private String name;
	
	
	@PostConstruct
	public void initController(){
		listOfCandidates=candidateDAO.getAllCadidates();
		name="";
	}
	
	public void addCandidate(ActionEvent event){
		try{
			if(name=="" || name==null){
				FacesContext context = FacesContext.getCurrentInstance(); 
				context.addMessage(event.getComponent().getClientId(),new FacesMessage("Please, type a name for the candidate!"));
			}else if(name.split(" ").length<2){
				FacesContext context = FacesContext.getCurrentInstance(); 
				context.addMessage(event.getComponent().getClientId(),new FacesMessage("Please, type a valid name for the candidate. Ex: Thomas Tailor"));
			}else if(!candidateDTO.getEmail().contains("@")){
				FacesContext context = FacesContext.getCurrentInstance(); 
				context.addMessage(event.getComponent().getClientId(),new FacesMessage("Please, type a valid email for the candidate!"));
			}else{
				String t[]=name.split(" ");
				candidateDTO.setFirstName(t[0]);
				candidateDTO.setLastName(t[1]);
				candidateDAO.addCandidate(candidateDTO);
				
				name="";
				candidateDTO.setEmail("");
				listOfCandidates=candidateDAO.getAllCadidates();
				
				testerController.refreshListOfCandidates();
			}
		}catch(Exception e){
			FacesContext context = FacesContext.getCurrentInstance(); 
			context.addMessage(event.getComponent().getClientId(),new FacesMessage("Error adding candidate!"));
		}
	}
	
	public void deleteCandidates(ActionEvent event){
		if(checkbox.size()!=0){
			for(String s:checkbox){
				CandidateDTO ed=new CandidateDTO();
				ed.setId(Integer.parseInt(s));
				try {
					if(candidateDAO.CandidateHasInterviews(ed)==false)
						candidateDAO.deleteCandidate(ed);
					else{
						FacesContext context = FacesContext.getCurrentInstance(); 
						context.addMessage(event.getComponent().getClientId(),new FacesMessage("Could not delete candidate! Candidate has interviews!"));
					}
				} catch (Exception e) {
					FacesContext context = FacesContext.getCurrentInstance(); 
					context.addMessage(event.getComponent().getClientId(),new FacesMessage("Error deleting candidate!"));
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}
			checkbox.clear();
			listOfCandidates=candidateDAO.getAllCadidates();
		}else{
			FacesContext context = FacesContext.getCurrentInstance(); 
			String clientId = event.getComponent().getClientId(context); 
			context.addMessage( clientId, new FacesMessage("Please, select at least one candidate!") );
		}
	}
	
	/////GETERS/SETTERS
	
 
	
	
	public List<String> getCheckbox() {
		return checkbox;
	}

	public TesterController getTesterController() {
		return testerController;
	}

	public void setTesterController(TesterController testerController) {
		this.testerController = testerController;
	}

	public void setCheckbox(List<String> checkbox) {
		this.checkbox.addAll(checkbox);
	}

	public CandidateDTO getCandidateDTO() {
		return candidateDTO;
	}

	public void setCandidateDTO(CandidateDTO candidateDTO) {
		this.candidateDTO = candidateDTO;
	}

	public List<CandidateDTO> getListOfCandidates() {
		return listOfCandidates;
	}

	public void setListOfCandidates(List<CandidateDTO> listOfCandidates) {
		this.listOfCandidates = listOfCandidates;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	
}
